package com.bbg.amyam.web.util.view;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bbg.amyam.web.util.menu.MenuItem;

public class ViewFilesGenerator extends AbstractGenerator {

	private static final Log log = LogFactory.getLog(ViewFilesGenerator.class);

	private String[] extensions = { "jspx", "xml" };

	public ViewFilesGenerator(MenuItem item, boolean overwrite) {
		super(item, overwrite);
	}

	@Override
	public void execute() throws IOException {
		String targetParentDir = super.getViewDirPath()+"amyamviews"+File.separator;
		String viewTemplateDir = super.getViewTemplateDir();

		String module = this.menuItem.getTargetParent();
		String targetDir = targetParentDir+this.menuItem.getTarget()+ File.separator;

		String templateFileDir = viewTemplateDir + "category" + File.separator
				+ "module" + File.separator;
		
		if (this.menuItem.getParent() != null) {
			if (this.menuItem.getTarget() != null
					&& this.menuItem.getTarget().equalsIgnoreCase(
							this.menuItem.getParent().getTarget())) {
				log.debug("Ignore menu item " + this.menuItem.getId()
						+ " since it has same target with its parent ");
				return;
			}			
			if("webflow".equalsIgnoreCase(this.menuItem.getType())){
				templateFileDir += "webflow" + File.separator;
			}else{
				templateFileDir += "menu" + File.separator;
			}
			
		}

		this.replacedValues.put("__module__name__", module);
		this.replacedValues.put("__menu__item__id__", this.menuItem.getId());
		this.replacedValues.put("__target__name__",
				StringUtils.replace(this.menuItem.getTarget(), "*", ""));

		log.debug("Copying files for menu item " + this.menuItem.getId()
				+ " from " + templateFileDir + " to " + targetDir);

		File srcDir = new File(templateFileDir);
		File destDir = new File(targetDir);

		Collection<File> srcFiles = FileUtils.listFiles(srcDir, extensions,
				false);
		if(this.menuItem.getType()!=null && "webflow".equalsIgnoreCase(this.menuItem.getType())){
			String viewsRootDir = super.getViewDirPath();
			File viewsrcDir=new File(viewsRootDir);
			WebflowViewFilesCopier copier = new WebflowViewFilesCopier(this.menuItem,srcDir,destDir, viewsrcDir);
			copier.doCopy(this.overwrite);
		}
		if (this.menuItem.getDomain() == null) {
			for (File src : srcFiles) {
				copyFile(src, destDir);
			}
		}else{
			String viewsRootDir = super.getViewDirPath();
			File viewsrcDir=new File(viewsRootDir);
			copyStandardViewFiles(srcDir,destDir,viewsrcDir);
		}
	}

	private void copyStandardViewFiles(File templateDir,File destDir,File viewsrcDir) throws IOException {
		StandardViewFilesCopier copier = new StandardViewFilesCopier(this.menuItem,templateDir,destDir, viewsrcDir);
		copier.doCopy(this.overwrite);
	}

	private void copyFile(File srcFile, File destDir) throws IOException {
		log.debug("Copying " + srcFile.getPath() + File.separator
				+ srcFile.getName() + " to " + destDir.getName());

		File targetFile = new File(destDir, srcFile.getName());
		if (targetFile.exists()) {
			if (!this.overwrite) {
				log.debug("Did not copy " + srcFile.getName()
						+ " since it exists");
				return;
			} else {
				targetFile.delete();
				log.debug("Deleted existing " + srcFile.getName());
			}
		}

		List<String> lines = FileUtils.readLines(srcFile);
		ArrayList<String> newLInes = new ArrayList<String>();
		for (String line : lines) {
			String newLine = processLine(line);
			newLInes.add(newLine);
		}

		FileUtils.writeLines(targetFile, newLInes);
		log.debug("Copied " + srcFile.getName() + " to " + destDir.getName());

	}

}
